package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.BandingAlipayContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.AlipayInfo;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class BandingAlipayPresenter extends BandingAlipayContract.Presenter {

    @Override
    public void findAlipay(String id) {
        mModel.findAlipay(id).subscribe(new SubscriberImpl<BaseResult<AlipayInfo>>() {
            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(BaseResult<AlipayInfo> alipayInfoBaseResult) {
                mView.setAlipayInfo(alipayInfoBaseResult.getData());
            }
        });
    }

    @Override
    public void bindAlipay(MultipartBody body) {
        mModel.bindAlipay(body)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        Logg.e(e.toString());
                        e.printStackTrace();
                        ToastUtils.showShort("绑定异常");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setBandingSuc();
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public MultipartBody bindAlipay(String accountId, String alipayId, String alipayName, String wechatNumber, String wechatName,String openBank, String realName, String bankCard, File alipayIma, File wechatImg) {
        MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("accountId", null, RequestBody.create(FORM_CONTENT_TYPE, accountId));
        builder.addFormDataPart("alipayId", null, RequestBody.create(FORM_CONTENT_TYPE, alipayId));
        builder.addFormDataPart("alipayName", null, RequestBody.create(FORM_CONTENT_TYPE, alipayName));
        builder.addFormDataPart("wechatNumber", null, RequestBody.create(FORM_CONTENT_TYPE, wechatNumber));
        builder.addFormDataPart("wechatName", null, RequestBody.create(FORM_CONTENT_TYPE, wechatName));
        builder.addFormDataPart("realName", null, RequestBody.create(FORM_CONTENT_TYPE, realName));
        builder.addFormDataPart("openBank", null, RequestBody.create(FORM_CONTENT_TYPE, openBank));
        builder.addFormDataPart("bankCard", null, RequestBody.create(FORM_CONTENT_TYPE, bankCard));
        if (alipayIma != null) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), alipayIma);
            builder.addFormDataPart("alipayImg", alipayIma.getName(), requestBody);
        }

        if (wechatImg != null) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), wechatImg);
            builder.addFormDataPart("wechatImg", wechatImg.getName(), requestBody);
        }


        MultipartBody multipartBody = builder.build();

        return multipartBody;
    }

    @Override
    public MultipartBody modifyAlipay(String accountId, String alipayId, String alipayName, String reason, String wechatNumber, String wechatName,String openBank, String realName, String bankCard, File alipayIma, File wechatImg) {
        MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("accountId", null, RequestBody.create(FORM_CONTENT_TYPE, accountId));
        builder.addFormDataPart("alipayId", null, RequestBody.create(FORM_CONTENT_TYPE, alipayId));
        builder.addFormDataPart("reason", null, RequestBody.create(FORM_CONTENT_TYPE, reason));
        builder.addFormDataPart("alipayName", null, RequestBody.create(FORM_CONTENT_TYPE, alipayName));
        builder.addFormDataPart("wechatNumber", null, RequestBody.create(FORM_CONTENT_TYPE, wechatNumber));
        builder.addFormDataPart("wechatName", null, RequestBody.create(FORM_CONTENT_TYPE, wechatName));
        builder.addFormDataPart("realName", null, RequestBody.create(FORM_CONTENT_TYPE, realName));
        builder.addFormDataPart("openBank", null, RequestBody.create(FORM_CONTENT_TYPE, openBank));
        builder.addFormDataPart("bankCard", null, RequestBody.create(FORM_CONTENT_TYPE, bankCard));
        if (wechatImg != null) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), wechatImg);
            builder.addFormDataPart("wechatImg", wechatImg.getName(), requestBody);
        }

        if (alipayIma != null) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), alipayIma);
            builder.addFormDataPart("alipayImg", alipayIma.getName(), requestBody);
        }

        MultipartBody multipartBody = builder.build();

        return multipartBody;
    }

    @Override
    public void modifyAlipay(MultipartBody body) {
        mModel.modifyAlipay(body)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("提交申请失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setUpdateSuc();
                        }
                        ToastUtils.showShort(baseResult.getMsg());
                    }
                });

    }

}
